"""
管理后台数据模型
基于RBAC权限模型：Users（管理员）-> Role（角色）-> Resource（资源/菜单）
"""
from tortoise.models import Model
from tortoise import fields


class BaseModel(Model):
    """基础模型（抽象类）"""
    create_time = fields.DatetimeField(auto_now_add=True, description='创建时间')
    update_time = fields.DatetimeField(auto_now=True, description='更新时间')
    
    class Meta:
        abstract = True


class Resource(BaseModel):
    """资源/菜单表（树形结构）"""
    id = fields.IntField(pk=True)
    name = fields.CharField(max_length=255, description='资源名称')
    code = fields.CharField(max_length=100, unique=True, description='资源代码（如：user:list）')
    type = fields.IntField(default=1, description='类型：1菜单 2按钮 3接口')
    path = fields.CharField(max_length=255, null=True, description='前端路由路径')
    icon = fields.CharField(max_length=100, null=True, description='图标')
    sort = fields.IntField(default=0, description='排序（数字越小越靠前）')
    status = fields.IntField(default=1, description='状态：0禁用 1启用')
    pid = fields.ForeignKeyField(
        'models.Resource', 
        related_name='children', 
        null=True, 
        description='父级资源ID'
    )
    
    class Meta:
        table = 'admin_resources'


class Role(BaseModel):
    """角色表"""
    id = fields.IntField(pk=True)
    name = fields.CharField(max_length=255, unique=True, description='角色名称')
    code = fields.CharField(max_length=100, unique=True, description='角色代码（如：admin）')
    description = fields.TextField(null=True, description='角色描述')
    status = fields.IntField(default=1, description='状态：0禁用 1启用')
    resources = fields.ManyToManyField(
        'models.Resource', 
        related_name='roles', 
        description='角色拥有的资源'
    )
    
    class Meta:
        table = 'admin_roles'


class AdminUser(BaseModel):
    """管理员用户表"""
    id = fields.IntField(pk=True)
    username = fields.CharField(max_length=64, unique=True, description='用户名')
    password = fields.CharField(max_length=255, description='密码（Bcrypt加密）')
    real_name = fields.CharField(max_length=50, null=True, description='真实姓名')
    email = fields.CharField(max_length=255, null=True, description='邮箱')
    phone = fields.CharField(max_length=20, null=True, description='手机号')
    avatar = fields.CharField(max_length=500, null=True, description='头像URL')
    status = fields.IntField(default=1, description='状态：0禁用 1启用')
    last_login_time = fields.DatetimeField(null=True, description='最后登录时间')
    last_login_ip = fields.CharField(max_length=50, null=True, description='最后登录IP')
    role = fields.ForeignKeyField(
        'models.Role', 
        related_name='users', 
        description='用户角色'
    )
    
    class Meta:
        table = 'admin_users'


class OperationLog(BaseModel):
    """操作日志表"""
    id = fields.IntField(pk=True)
    user = fields.ForeignKeyField(
        'models.AdminUser', 
        related_name='logs', 
        description='操作用户'
    )
    action = fields.CharField(max_length=100, description='操作动作（如：create_user）')
    module = fields.CharField(max_length=100, description='操作模块（如：用户管理）')
    description = fields.TextField(null=True, description='操作描述')
    ip = fields.CharField(max_length=50, null=True, description='操作IP')
    user_agent = fields.CharField(max_length=500, null=True, description='浏览器信息')
    request_data = fields.JSONField(null=True, description='请求参数')
    response_data = fields.JSONField(null=True, description='响应数据')
    status = fields.IntField(default=1, description='状态：1成功 0失败')
    error_msg = fields.TextField(null=True, description='错误信息')
    
    class Meta:
        table = 'admin_operation_logs'


# 兼容旧代码的别名
Users = AdminUser
